<template>
  <div class="quotes-container">
    <div class="header">
      <el-button @click="dialogVisible = true" type="success" style="margin-bottom: 10px">添加</el-button>
    </div>
    <!-- 表格 -->
    <div class="table">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        height="750">
        <el-table-column
          prop="id"
          label="编号"
          type="index"
          width="150">
        </el-table-column>
        <el-table-column
          prop="text"
          label="内容">
        </el-table-column>
        <el-table-column
          prop="author"
          label="作者">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row.id)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- dialog 弹出表单 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
        <el-form-item label="内容">
          <el-input v-model="formLabelAlign.text"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="formLabelAlign.author"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false;addQuote()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getQuotes,postQuote,deleteQuote} from '../../api/admin/quote.js'
export default {
  data() {
    return{
      tableData: [],
      dialogVisible: false,
      labelPosition: 'left',
      formLabelAlign: {
        text: '',
        author: '',
      }
    }
  },
  methods: {
    handleClick(id) {
      deleteQuote(id).then( (res) => {
        console.log(res.data);
        this.init()
      })
    },
    init() {
      getQuotes().then( (res) => {
        console.log(res.data);
        this.tableData = res.data.data
      })
    },
    addQuote() {
      postQuote(this.formLabelAlign).then( () => {
        // console.log(res.data);
        this.init()
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(() => {
          done();
        })
        .catch(() => {});
    }
  },
  created() {
    this.init()
  }
}
</script>

<style scoped>

</style>